package com.ruoyi.project.module.fbsHead.service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.constant.ImportExcelConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ImportExcelUtil;
import com.ruoyi.project.common.domain.ErrorCommon;
import com.ruoyi.project.module.fbsDetail.domain.FbsDetail;
import com.ruoyi.project.module.fbsDetail.mapper.FbsDetailMapper;
import com.ruoyi.project.module.products.domain.Products;
import com.ruoyi.project.module.products.mapper.ProductsMapper;
import com.ruoyi.project.module.productsUpload.domain.ProductsUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.module.fbsHead.mapper.FbsHeadMapper;
import com.ruoyi.project.module.fbsHead.domain.FbsHead;
import com.ruoyi.project.module.fbsHead.service.IFbsHeadService;
import com.ruoyi.common.support.Convert;

import static com.ruoyi.common.utils.file.DownloadXls.DOWNLOAD_TYPE_XLS;
import static com.ruoyi.common.utils.file.DownloadXls.DOWNLOAD_TYPE_XLSX;

/**
 * 转入FBS 服务层实现
 *
 * @author lcx
 * @date 2018-09-17
 */
@Service
public class FbsHeadServiceImpl implements IFbsHeadService {
    @Autowired
    private FbsHeadMapper fbsHeadMapper;
    @Autowired
    private FbsDetailMapper fbsDetailMapper;
    @Autowired
    private ProductsMapper productsMapper;

    /**
     * 查询转入FBS信息
     *
     * @param id 转入FBSID
     * @return 转入FBS信息
     */
    @Override
    public FbsHead selectFbsHeadById(Integer id) {
        return fbsHeadMapper.selectFbsHeadById(id);
    }

    /**
     * 查询转入FBS列表
     *
     * @param fbsHead 转入FBS信息
     * @return 转入FBS集合
     */
    @Override
    public List<FbsHead> selectFbsHeadList(FbsHead fbsHead) {

        if (StringUtils.isNotEmpty(fbsHead.getSearchValue())) {
            String[] searchRow = {
                    "f.Name",
                    "f.TFileUrl",
                    "f.TaskFileUrl"
            };
            return fbsHeadMapper.selectFbsHeadListAsSearchRow(fbsHead, searchRow);
        } else {
            return fbsHeadMapper.selectFbsHeadList(fbsHead);
        }
    }

    /**
     * 新增转入FBS
     *
     * @param fbsHead 转入FBS信息
     * @return 结果
     */
    @Override
    public int insertFbsHead(FbsHead fbsHead) {
        return fbsHeadMapper.insertFbsHead(fbsHead);
    }

    /**
     * 修改转入FBS
     *
     * @param fbsHead 转入FBS信息
     * @return 结果
     */
    @Override
    public int updateFbsHead(FbsHead fbsHead) {
        return fbsHeadMapper.updateFbsHead(fbsHead);
    }

    /**
     * 删除转入FBS对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteFbsHeadByIds(String ids) {
        int refal = 0;
        refal += fbsHeadMapper.deleteFbsHeadByIds(Convert.toStrArray(ids));
        refal += fbsDetailMapper.deleteFbsDetailByHeadId(Convert.toStrArray(ids));

        return refal;
    }

    @Override
    public List<ErrorCommon> uploadFbsHeader(List<FbsHead> phList, String download, FbsHead fbsHead) {

        int re_tem = 0;
        //错误返回对象
        List<ErrorCommon> errorList = new ArrayList();
        //导入的fbs集合
        List<FbsDetail> fbsDetailList = new ArrayList();

        //获取xls的内容行
        for (FbsHead fbsHead_tem : phList) {

            String[] splitResult = fbsHead_tem.getFileUrl().split("\\.");
            String extension = splitResult[splitResult.length - 1];

            //添加pdf文件名
            if (!DOWNLOAD_TYPE_XLSX.equals(extension)) {
                fbsHead.setTFileUrl(fbsHead_tem.getFileUrl());
            } else {
                //添加xls文件名
                fbsHead.setTaskFileUrl(fbsHead_tem.getFileUrl());

                File file = new File(download + fbsHead_tem.getFileUrl());  //根据文件路径获得File文件

                //获取xls数据
                try {
                    List<List<Object>> temList = ImportExcelUtil.importExcel_AsTwo(file);

                    for (int low = 2; low < temList.size(); low++) {
                        //获取行
                        List<Object> row = temList.get(low);
                        if ("".equals(row.get(0))){
                            continue;
                        }

                        //查询商品主SKU
                        Products ProductsTemp = productsMapper.selectProductsByMainSKU(String.valueOf(row.get(0)).trim());
                        //判断商品主SKU是否存在
                        if (ProductsTemp == null) {
                            reErrorRL(errorList, low, 0, row.get(0) + ImportExcelConstants.NO_EXIST);
                        }
                        //判断 FBS sku
                        if (row.get(1) == null) {
                            reErrorRL(errorList, low, 0, row.get(1) + ImportExcelConstants.IS_NULL);
                        }

                        //判断 数量
                        if (row.get(2) == null) {
                            reErrorRL(errorList, low, 0, row.get(2) + ImportExcelConstants.IS_NULL);
                        }

                        //判断 尺寸
                        if (row.get(3) == null) {
                            reErrorRL(errorList, low, 0, row.get(3) + ImportExcelConstants.IS_NULL);
                        }

                        //判断 重量
                        if (row.get(4) == null) {
                            reErrorRL(errorList, low, 0, row.get(4) + ImportExcelConstants.IS_NULL);
                        }

                        //判断型号
                        if (row.get(5) == null) {
                            reErrorRL(errorList, low, 0, row.get(5) + ImportExcelConstants.IS_NULL);
                        }

                        //判断upc
                        if (row.get(6) == null) {
                            reErrorRL(errorList, low, 0, row.get(6) + ImportExcelConstants.IS_NULL);
                        }

                        //判断品牌
                        if (row.get(7) == null) {
                            reErrorRL(errorList, low, 0, row.get(7) + ImportExcelConstants.IS_NULL);
                        }

                        //判断产品中文名称
                        if (row.get(8) == null) {
                            reErrorRL(errorList, low, 0, row.get(8) + ImportExcelConstants.IS_NULL);
                        }
                        //添加到集合
                        FbsDetail fbsDetail = new FbsDetail();

                        addFbsHead(fbsDetailList,fbsDetail,row,low);
                    }




                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        if (fbsDetailList.size() == 0){
            reErrorRL(errorList, 0, 0, ImportExcelConstants.PRODUCT_NUM_ERROR);

        }

        //有错误信息则不插入
        if (errorList.size() == 0) {
            //添加头
            fbsHeadMapper.insertFbsHead(fbsHead);

            //添加表格中的详细信息
            for (FbsDetail fbsDetail : fbsDetailList){
                fbsDetail.setHeadId(fbsHead.getId());
                re_tem += fbsDetailMapper.insertFbsDetail(fbsDetail);
            }
        }


        return errorList;
    }

    /**
     * @Author lcx
     * @Date 2018/9/18 11:39
     * 添加到订单
     */
    private List<FbsDetail> addFbsHead(List<FbsDetail> fbsDetailList,FbsDetail fbsDetail, List<Object> row,Integer low){

        fbsDetail.setLineNum(low);
        fbsDetail.setSKU(String.valueOf(row.get(0)).trim());
        fbsDetail.setFBSSKU(String.valueOf(row.get(1)).trim());
        fbsDetail.setQuantity(Integer.parseInt((String) row.get(2)));
        fbsDetail.setSize(String.valueOf(row.get(3)));
        fbsDetail.setWeight(String.valueOf(row.get(4)));
        fbsDetail.setModel(String.valueOf(row.get(5)));
        fbsDetail.setUPC(String.valueOf(row.get(6)));
        fbsDetail.setBrand(String.valueOf(row.get(7)));
        fbsDetail.setChineseName(String.valueOf(row.get(8)));

        fbsDetailList.add(fbsDetail);

        return fbsDetailList;
    }

    /**
     * @Author lcx
     * @Date 2018/8/25 11:00
     * 添加错误信息
     */
    private List<ErrorCommon> reErrorRL(List<ErrorCommon> errorLists, int low, int row, Object error) {
        ErrorCommon errorCommon = new ErrorCommon();

        errorCommon.setLow(low + 1);
        errorCommon.setRow(row + 1);
        errorCommon.setErrorValue(String.valueOf(error));

        errorLists.add(errorCommon);

        return errorLists;
    }
}
